package com.zhiy.com.estation.ui.fragment;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import com.zhiy.com.estation.Broadcast;
import com.zhiy.com.estation.R;
import com.zhiy.com.estation.fragmentTag.FragmentTag;
import com.zhiy.com.estation.manager.PreferenceManager;
import com.zhiy.com.estation.model.User;
import com.zhiy.com.estation.ui.MainActivity;
import com.zhiy.com.estation.ui.StartUpActivity;
import com.zhiy.com.estation.ui.base.BaseFragment;
import com.zhiy.com.estation.ui.fragment.login.LoginContract;
import com.zhiy.com.estation.ui.fragment.login.LoginModel;
import com.zhiy.com.estation.ui.fragment.login.LoginPresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Zyh on 2016/12/3.
 */
public class Fragment_Login extends BaseFragment implements LoginContract.View {
    @BindView(R.id.fra_login_name)
    EditText nameEt;
    @BindView(R.id.fra_login_password)
    EditText passworEt;
    private ProgressDialog mProgressDialog;
    @Override
    protected int getInflaterLayoutId() {
        return R.layout.fragment_login;
    }
    @Override
    protected void initView(View view) {
        mProgressDialog=new ProgressDialog(mBaseActivity);
        mProgressDialog.setMessage("正在请求");
    }
    @OnClick({R.id.fra_login_bt,R.id.fra_regis, R.id.fra_for_pass})
    public void loginClick(View view){
        switch (view.getId()){
            case R.id.fra_login_bt:
                login();
                break;
            case R.id.fra_regis:
                mBaseActivity.showFragment(Fragment_register.class, FragmentTag.REGISTER,null,true);
                break;
            case R.id.fra_for_pass:
                mBaseActivity.showFragment(Fragment_Find_Pass.class,FragmentTag.FIND_PASS,null,true);
                break;
            default:
                break;
        }
    }
    public void login(){
            String name=nameEt.getText().toString();
            String password=passworEt.getText().toString();
            Log.d("----","45"+name+"---"+password);
            if (TextUtils.isEmpty(name)||TextUtils.isEmpty(password)){
                Toast.makeText(mContext,"输入内容不能为空",Toast.LENGTH_SHORT).show();
            }else {
                //不为空则进行网路登录请求
                LoginPresenter presenter=new LoginPresenter();
                presenter.setMV(new LoginModel(),this);
                presenter.login(name,password);
                mProgressDialog.show();
        }
    }
    @Override
    public void isLoginSuccess(User user) {
        //登录成功保存用户的信息
        mProgressDialog.dismiss();
        //保存登录信息状态
        mPreferen.saveToAutoLogin(true);
        //保存用户的信息
        String name=user.getName();
        String phone=user.getPhone();
        String email=user.getEmail();
        String avar=user.getAvar();
        int userid=user.getUserid();
        if (!TextUtils.isEmpty(userid+"")) {
            mPreferen.saveToUserid(userid);
        }
            mPreferen.saveToName(name);


            mPreferen.saveToPhone(phone);



            mPreferen.saveToEmail(email);



            mPreferen.saveToavar(avar);

        mBaseActivity.sendBroadcast(new Intent(RECEIVER));
        popBackStack();
//        //fragment中跳转需要使用fragment对应的getActicity
//        startActivity(new Intent(mBaseActivity,MainActivity.class));
//        mBaseActivity.finish();
//        //ovrridePendingTransition--切换activity的添加动画使用
//        //第一个参数是：第二个Activity进来的动画，第二个是第一个activity退出的动画
//        mBaseActivity.overridePendingTransition(R.anim.fragment_enter,R.anim.fragment_exit);
    }
    @Override
    public void isFailure() {
        mProgressDialog.dismiss();
        Toast.makeText(mContext,"网络请求失败",Toast.LENGTH_SHORT).show();
    }
    @Override
    public void showMsg(String msg) {
        Log.d("----",msg);
    }
}
